package com.example.waibao.controller;

import com.example.waibao.Component.UserHelper;
import com.example.waibao.service.SseEmitterService;
import com.example.waibao.service.UserService;
import com.example.waibao.service.impl.SseEmitterServer;
//import com.example.waibao.service.impl.SseEmitterServer2;
import com.example.waibao.service.taskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
//@RequestMapping("/sse")
public class SseEmitterController {


    @Autowired
    private taskService taskService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserHelper userHelper;

    //sse连接接口
//    @GetMapping (value = "/sse/connect/{id}")
//    public SseEmitter connect(@PathVariable String id){
//        return SseEmitterServer2.connect(id);
//    }

//    @GetMapping (value = "/sse/connect")
//    public SseEmitter connect(@RequestParam String id){
//        return SseEmitterServer2.connect(id);
//    }

    @GetMapping (value = "/sse/connect")
    public SseEmitter connect(@RequestParam String id, int school_Id){
        //@RequestParam String id, int school_Id
        //按学校连接学生
        SseEmitterServer sseEmitterServer = new SseEmitterServer();
//        return sseEmitterServer.subscribe(id, school_Id);

        return sseEmitterServer.subscribe(id);
    }

    //sse向指定用户发送消息接口
    @GetMapping (value = "/sse/send")
    public Map<String,Object> send(@RequestParam String id, @RequestParam(value = "message", required = false) String message) throws IOException {
//        Map<String,Object> returnMap = new HashMap<>();
//        //向指定用户发送信息
//        SseEmitterServer2.sendMessage(id,message);

        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        Map<String,Object> returnMap = new HashMap<>();
        //向指定用户发送信息
        sseEmitterServer.sendOneClientMessage(id,message);

        returnMap.put("msg","向id为"+id+"的用户发送："+message+"成功!");
        returnMap.put("code","200");
        returnMap.put("data",null);



        return returnMap;
    }



    @GetMapping (value = "/sse/send_box")
    public Map<String,Object> send_box(@RequestParam String id, @RequestParam(value = "message", required = false) String message) throws IOException {
//        Map<String,Object> returnMap = new HashMap<>();
//        //向指定用户发送信息
//        SseEmitterServer2.sendMessage(id,message);
        SseEmitterServer sseEmitterServer = new SseEmitterServer();

        if(message.equals("恭喜获得等级奖励")){
            taskService.update_student_level(Integer.parseInt(id));
            sseEmitterServer.sendOneClientMessage(id,message);
        }else if(message.equals("恭喜获得隐藏头像框")){
            String name = userService.check_head_have(Integer.parseInt(id));
            if (name==null){
                taskService.update_head_heaver(Integer.parseInt(id));
                sseEmitterServer.sendOneClientMessage(id, message);
            }
        }else {
            message = "特殊奖励:"+message;
            sseEmitterServer.sendOneClientMessage(id,message);
        }

        Map<String,Object> returnMap = new HashMap<>();
        //向指定用户发送信息
        returnMap.put("msg","向id为"+id+"的用户发送："+message+"成功!");
        returnMap.put("code","200");
        returnMap.put("data",null);

        return returnMap;
    }



    //sse向所有已连接用户发送消息接口
    @GetMapping (value = "/sse/batchSend")
    public Map<String,Object> batchSend(@RequestParam(value = "message", required = false) String message){
        Map<String,Object> returnMap = new HashMap<>();
        //向指定用户发送信息
        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        sseEmitterServer.sendAllClientMsg(message);
        returnMap.put("msg","发布公告成功");
        returnMap.put("code","200");
        returnMap.put("data",message);
        return returnMap;
    }

//    sse关闭接口
//    @GetMapping (value = "/sse/close/{id}")
//    public Map<String,Object> close(@PathVariable String id){
//        Map<String,Object> returnMap = new HashMap<>();
//        //移除id
//        SseEmitterServer2.removeUser(id);
//        System.out.println("当前连接用户id:"+SseEmitterServer2.getIds());
//        returnMap.put("message","连接关闭成功!");
//        returnMap.put("status","200");
//        returnMap.put("result",null);
//        return returnMap;
//    }

    @GetMapping (value = "/sse/close")
    public Map<String,Object> close(@RequestParam String id){
//        Map<String,Object> returnMap = new HashMap<>();
//        //移除id
//        SseEmitterServer2.removeUser(id);

        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        Map<String,Object> returnMap = new HashMap<>();
        //移除id
        sseEmitterServer.close(id);

//        System.out.println("当前连接用户id:"+SseEmitterServer2.getIds());
        returnMap.put("message","连接关闭成功!");
        returnMap.put("status","200");
        returnMap.put("result",null);
        return returnMap;
    }


//    @Autowired
//    private SseEmitterService sseEmitterService;
//
//    /**
//     * 创建SSE长链接
//     *
//     * @param clientId   客户端唯一ID(如果为空，则由后端生成并返回给前端)
//     * @return org.springframework.web.servlet.mvc.method.annotation.SseEmitter
//     * @author re
//     * @date 2021/12/12
//     **/
//    @CrossOrigin //如果nginx做了跨域处理，此处可去掉
//    @GetMapping("/CreateSseConnect")
//    public SseEmitter createSseConnect(@RequestParam(name = "clientId", required = false) String clientId) {
//        return sseEmitterService.createSseConnect(clientId);
//    }
//
//    /**
//     * 关闭SSE连接
//     *
//     * @param clientId 客户端ID
//     * @author re
//     * @date 2021/12/13
//     **/
//    @GetMapping("/CloseSseConnect")
//    public Result closeSseConnect(String clientId) {
//        sseEmitterService.closeSseConnect(clientId);
//        return ResultGenerator.genSuccessResult(true);
//    }


}
